package com.cxs.service.impl;

import com.cxs.contents.DocterContent;
import com.cxs.enums.HospitalErrorResultEnum;
import com.cxs.exception.HospitalException;
import com.cxs.mapper.DoctorMapper;
import com.cxs.pojo.Doctor;
import com.cxs.pojo.DoctorExample;
import com.cxs.service.DocterService;
import com.cxs.vo.DocterSheduleVO;
import com.cxs.vo.ReportDocterSelectVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/10/19  15:34
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Slf4j
@Service
public class DocterServiceImpl implements DocterService {
    @Autowired
    private DoctorMapper doctorMapper;

    /**
     * 按星期查询医生的排班信息
     *
     * @param day
     * @return
     */
    public List<DocterSheduleVO> sheduleList(String day) {
        if (!DocterContent.SHICHEDULE_DAY.contains(day)) {
            log.info("【排班信息】:输入的信息不符合规范,day = {}", day);
            throw new HospitalException(HospitalErrorResultEnum.DOCTER_SHEDULE_ERROR.getCode(), "【排班信息】:输入的信息不符合规范");
        }

        List<Doctor> doctorList = doctorMapper.selectDockerShichedule(day);
        List<DocterSheduleVO> list = new ArrayList<>();
        doctorList.forEach(doctor -> {
            DocterSheduleVO vo = new DocterSheduleVO();
            BeanUtils.copyProperties(doctor, vo);
            list.add(vo);
        });
        return list;
    }

    @Override
    public List<ReportDocterSelectVO> getList(Integer registeredId, Integer departmentId) {
        List<ReportDocterSelectVO> list = new ArrayList<>();
        DoctorExample example = new DoctorExample();

        DoctorExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(registeredId)) {
            criteria.andRegisteredIdEqualTo(registeredId);
        }

        if (!StringUtils.isEmpty(departmentId)) {
            criteria.andDepartmentIdEqualTo(departmentId);
        }

        List<Doctor> doctors = doctorMapper.selectByExample(example);

        doctors.forEach(d -> {
            ReportDocterSelectVO vo = new ReportDocterSelectVO();
            BeanUtils.copyProperties(d, vo);
            list.add(vo);
        });

        return list;
    }
}
